package com.hiedu.service.impl;

import com.hiedu.beans.dto.ProductAddDTO;
import com.hiedu.beans.dto.ProductUpdateDTO;
import com.hiedu.beans.entity.Product;
import com.hiedu.commons.ServiceCode;
import com.hiedu.commons.ServiceException;
import com.hiedu.mapper.ProductMapper;
import com.hiedu.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ProjectName: hi-app
 * @Titile: ProductServiceImpl
 * @Author: 86180
 * @Description: TODO
 */
@Service
@Transactional
@Slf4j
public class ProductServiceImpl implements ProductService {
    //自动注入商品持久层对象
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> getProductList() {
        List<Product> productList = productMapper.getProductList();
        if (productList == null || productList.size() == 0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"商品列表为空");
        }
        return productList;
    }

    @Override
    public Product getProductById(Integer id) {
        if(id==null){
            throw new RuntimeException("商品ID不能为空");
        }
        if (id < 1){
            throw new RuntimeException("商品ID参数有误");
        }
        Product product = productMapper.getProductById(id);
        if (product == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"商品列表为空");
        }
        //打印商品
        log.debug("商品信息：{},查询到商品信息{}",id,product);
        return product;
    }

    @Override
    public void deleteProductById(Integer id) {
        if (id == null){
            throw new RuntimeException("商品ID不能为空");
        }
        Product product = productMapper.getProductById(id);
        if (product == null||product.getId()==null){
            throw new RuntimeException("商品不存在");
        }
        //执行删除操作
        //返回受影响函数
        //alt+shift+z 报错
        int result = 0;
        try {
            result = productMapper.deleteProductById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (result < 1){
            throw new RuntimeException("删除商品失败");
        }
        else {
            System.out.println("删除商品成功");
        }
    }

    @Override
    public void add(ProductAddDTO productAddDTO) {
        // 新曾商品的数据进行合法校验，后期利用框架完成
        Product product = new Product();
        product.setName(productAddDTO.getName());
        product.setPrice(productAddDTO.getPrice());
        product.setNum(productAddDTO.getNum());
        //进行新曾操作
        int result = productMapper.insert(product);
        if (result < 1) {
            throw new ServiceException(ServiceCode.ERROR_NEW_ADD,"新增商品失败");
        }

    }

    @Override
    public void update(ProductUpdateDTO productUpdateDTO) {
        //todo 对修改商品的数据进行合法校验
        if(productUpdateDTO ==  null || productUpdateDTO.getId() == null){
            throw new RuntimeException("商品信息不能为空");
        }
        //构建PRODUCT对象
        Product product = new Product();
        product.setName(productUpdateDTO.getName());//ctrl + alt +向下
        product.setPrice(productUpdateDTO.getPrice());
        product.setNum(productUpdateDTO.getNum());
        product.setId(productUpdateDTO.getId());

        int result = 0;
        //进行修改操作
        try {
             result = productMapper.update(product);
        } catch (Exception e) {
            throw new RuntimeException("更新商品失败");
        }
        if(result< 1){
            throw new RuntimeException("更新商品失败");
        }

    }
}
